/**
 * Created by chenzui on 2017/4/7.
 */

var $articleTitle = $('#article-title');
var $articleDesc = $('#article-desc');
var imgUrl = "";
function addArticle() {
    var param = {
        title:$articleTitle.val().trim(),
        desc:$articleDesc.val().trim(),
        imgUrl:imgUrl,
        content:editor.$txt.html()
    }
    Ajax.ajax({
        url: "/article/addArticle",
        data: param,
        type: "post",
        dataType: 'json',
        success:function(res){
            console.log(res);
            alert("发布成功");
            $('#mainframe').load('/shop/article/list.html');
        }

    });

}
$(function () {
    $('#submitBtn').on('click',function () {
        addArticle();
    });

    $("#file_sequence_id_1").change(function(){
        var file = this.files[0];
        if(!/image\/\w+/.test(file.type)){
            alert("文件必须为图片！");
            return false;
        }

        var tmp = $(this);
        var id = tmp.attr("id");
        $.ajaxFileUpload({
            url:'/file/upload',            //需要链接到服务器地址
            fileElementId:tmp.attr("id"),
            secureuri: false,
            dataType: 'json',                                                //服务器返回的格式，可以是json
            date:$('#updateForm').serialize(),
            // async:true,
            success: function (data,status){                                 //相当于java中try语句块的用法
                imgUrl = data.data;
            },
            error: function (data, status, e){                               //相当于java中catch语句块的用法

            }
        });
        $('#'+id+'').before(tmp);
        $('#'+id+'').remove();

        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function(e){
            $("#imageList").empty();
            var img  = $("<li><img src='"+this.result+"'></li>");
            $("#imageList").append(img);
        }

    });
})